from rest_framework.serializers import *
from xj_users.models import Label, Question, User, Reply, Article



'''用户序列化器'''
class UserSerializerSimple(ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')



'''标签序列化器'''
class LabelModelSerializer(ModelSerializer):

    class Meta:
        model = Label
        fields = '__all__'



'''用户关注标签序列化器'''
class LabelsSerializer(ModelSerializer):

    class Meta:
        model = Label
        fields = ('id', 'label_name')



'''问答列表序列化器'''
class QuestionModelSerializer(ModelSerializer):
    user = StringRelatedField(read_only=True)
    labels = StringRelatedField(read_only=True, many=True)

    class Meta:
            model = Question
            exclude = ('content','thumbup','solve','updatetime',)



'''问题发布序列化器'''
class PubQuestionModelSerializer(ModelSerializer):

    class Meta:
        model = Question
        fields = '__all__'



'''评论的序列化器'''
class ReplySerializerForSubAndParent(ModelSerializer):

    user = UserSerializerSimple(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count","unuseful_count","user"]


'''评论列表的序列化器'''
class ReplySerializerList(ModelSerializer):

    user = UserSerializerSimple(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent = ReplySerializerForSubAndParent(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]


'''问题详情序列化器'''
class QuestionDetailSerializer(ModelSerializer):

    user = StringRelatedField(read_only=True)
    labels = StringRelatedField(read_only=True, many=True)
    comment_question = ReplySerializerList(read_only=True, many=True)
    comment_reply = ReplySerializerList(read_only=True, many=True)
    answer_question = ReplySerializerList(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname",
                  "replytime","title","unuseful_count","useful_count",
                  "user","visits","content","comment_question","comment_reply",
                  "answer_question"]



'''回答问题'''
class ReplyQusetionSerializer(ModelSerializer):
    class Meta:
        model=Reply
        fields=('problem','content','type','parent','user')
        extra_kwargs = {
            'user': {'write_only': True}
        }



'''获取某个标签的详情'''
class LabelArticlesInfoSerializer(ModelSerializer):
    class Meta:
        model=Article

        fields=('id','collected_users','content','createtime','image',
                'title','user','visits')



class LabelQuDetailInfoSerializer(ModelSerializer):
    class Meta:
        model=Question
        fields=('id','createtime','labels','reply','replyname','replytime',
                'title','unuseful_count','useful_count','user','visits')


class LabelsDetailInfoSerializer(ModelSerializer):
    questions=LabelQuDetailInfoSerializer(label='标签下的问题',many=True)
    articles=LabelArticlesInfoSerializer(label='标签下的所有文章',many=True)
    class Meta:
        model=Label
        fields=('id','baike_url','desc','label_icon','label_name','users','questions','articles')

class ReplySerializerForCreate(ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'